<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Packchannel extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/packchannel/manager');
	}
	
	//Quan ly goi kenh
	public function manager(){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->headTitle('Quản lý gói kênh - Admin Panel');
		
		$currentPage = $this->input->get('page',true);
		$currentPage = (!empty($currentPage))?$currentPage:'1';
		$this->load->model('ServicePack_Model');
		$countpack = $this->ServicePack_Model->countServicePack();
		$this->loadData('countpack',$countpack);
		$pagingConfig = $this->init_pagination('administrator/packchannel/manager?act=paging', $countpack, $per_page, $num_link);
		$this->loadData('paging', $this->pagination->create_links());
		$this->loadData('listservicepack', $this->ServicePack_Model->getServicePackLimit((($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
		
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('paging','');
				$this->loadData('listservicepack', $this->ServicePack_Model->searchServicePack($keysearch));
			}

		}
		$this->loadView('administrator/packchannel/manager');
	
	}
	//Them goi kenh truyen hinh
	public function add(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$errorupload = "";
		
		$this->load->model('Channel_Model');
		$this->loadData('listchannels', $this->Channel_Model->getAllChannel());
		if($this->input->post()){
			if($this->form_validation->run('servicepack') == true){							
				$upload = $this->upload_image_process('Image', 'uploads/servicepack/');
				if(isset($upload['error_upload'])){	
					$errorupload = "<p>".$upload['error_upload']."</p>";
				}else{
					$this->load->model('ServicePack_Model');
					$url_image = $upload['image_full_url'];
					$insert_id = $this->ServicePack_Model->insertServicepack($url_image);
					$listchannel = $this->input->post('listchannel', true);
					$this->load->model('ServicePackDetail_Model');
					$this->load->model('ServicePackChannel_Model');
					$this->ServicePackDetail_Model->insertServicePackDetail($insert_id);
					foreach($listchannel as $item){
						$this->ServicePackChannel_Model->insertServicePackChannel($insert_id, $item);
					}
					redirect('administrator/packchannel/manager');	
				}
			}		
		}
		$this->loadData('errorupload',$errorupload);
		$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
		$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
		$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
		$this->loadScript(base_url('js/jquery.maskMoney.js'));
		$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
		$this->loadScript(base_url('js/jquery.customFile.js'));
		$this->loadScript(base_url('js/admin/jquery.image.js'));
		$this->loadScript(base_url('js/admin/jquery.addprice.js'));
		$this->headTitle('Thêm gói kênh - Admin Panel');
		$this->loadView('administrator/packchannel/add');

	}
	
	//Sua goi kenh truyen hinh
	public function modify(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Channel_Model');	
		$this->load->model('ServicePack_Model');
		$this->load->model('ServicePackDetail_Model');
		$this->load->model('ServicePackChannel_Model');	
		$spid = $this->input->get('spid', true);
		$servicepack = $this->ServicePack_Model->getServicePackBySpId($spid);
		$channelofpack = $this->ServicePackChannel_Model->getServicePackChannelBySpId($spid);
		foreach($channelofpack as $item){
			$arrchannelofpack[] = $item->ChannelId;
		}
		$servicepackdetail = $this->ServicePackDetail_Model->getServicePackDetailBySpId($spid);
		foreach($servicepackdetail as $item){
			$arrayDuration[] = $item->Duration;
		}
		if($servicepack){
			$errorupload = "";
			if($this->input->post()){
				if($this->form_validation->run('servicepack') == true){	
					if(count($this->input->post('Duration', true)) > 0){
						$url_image = $this->input->post('hidden_image', true);						
						if($_FILES['Image']['error'] != 4){
							$upload = $this->upload_image_process('Image', 'uploads/servicepack/');
							if(isset($upload['error_upload'])){
								$errorupload = "<p>".$upload['error_upload']."</p>";
							}else{	
								if(file_exists($url_image)){
									unlink($url_image);
								}
								$url_image = $upload['image_full_url'];	
							}
						}
						$this->ServicePack_Model->updateServicePack($spid, $url_image);
						
						$Duration = $this->input->post('Duration', true);
						$arraydiff2 = array_diff($arrayDuration, $Duration);
						if(!empty($arraydiff2)){
							foreach($arraydiff2 as $item2){
								$this->ServicePackDetail_Model->deleteSPDByDuration($item2);
							}
						}else{
							$this->ServicePackDetail_Model->insertServicePackDetail($spid);
						}
						$listchannel = $this->input->post('listchannel',true);
						$arraydiff = array_diff($arrchannelofpack, $listchannel);
						if(!empty($arraydiff)){
							foreach($arraydiff as $item){
								$this->ServicePackChannel_Model->deleteSPCByChannelId($item);
							}
						}else{
							foreach($listchannel as $item){
								$this->ServicePackChannel_Model->insertServicePackChannel($spid, $item);
							}
						}
						redirect('administrator/packchannel/manager');
					}else{
						$errorPrice = "<p>Trường giá cước không được bỏ trống.</p>";
					}
				}		
			}
			$listchannels = $this->Channel_Model->getAllChannel();
			$this->loadData('listchannels',$listchannels);
			$this->loadData('servicepack', $servicepack);
			$this->loadData('servicepackdetail', $servicepackdetail);
			$this->loadData('channelofpack', $channelofpack);
			$this->loadData('errorupload',$errorupload);
			$this->loadStylesheet(base_url('plugins/chosen/chosen.css'));
			$this->loadScript(base_url('plugins/chosen/chosen.jquery.min.js'));
			$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
			$this->loadScript(base_url('js/jquery.maskMoney.js'));
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->loadScript(base_url('js/jquery.customFile.js'));
			$this->loadScript(base_url('js/admin/jquery.image.js'));
			$this->loadScript(base_url('js/admin/jquery.addprice.js'));
			$this->headTitle('Sửa gói kênh - Admin Panel');
			$this->loadView('administrator/packchannel/modify');
		}else{
			redirect('administrator/packchannel/manager');
		}
	}
	//Doi trang thai goi kenh
	public function change_status(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('ServicePack_Model');
			$changeStatus = $this->ServicePack_Model->changeStatusServicePack($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa goi kenh
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('ServicePack_Model');		
			if($this->ServicePack_Model->deleteServicePack($key)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
}